package com.orkhonsoft.tengrinshar.chat.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.orkhonsoft.tengrinshar.chat.entity.FriendApplyEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.orkhonsoft.tengrinshar.chat.entity.FriendEntity;
import com.orkhonsoft.tengrinshar.chat.entity.UserEntity;
import com.orkhonsoft.tengrinshar.chat.mapper.FriendMapper;
import com.orkhonsoft.tengrinshar.chat.mapper.UserMapper;



@Service("friendService")
public class FriendService {
	@Autowired
	private FriendMapper friendmapper;
	@Autowired
	private UserService userService;

	public FriendEntity queryObject(Long id){
		return friendmapper.queryObject(id);
	}
	
	public List<FriendEntity> queryList(Map<String, Object> map){
		return friendmapper.queryList(map);
	}
	
	public int queryTotal(){
		return friendmapper.queryTotal();
	}
	
	public void save(FriendEntity friend){
		FriendEntity exist = queryByFriend(friend);
		if (exist == null) {
			friendmapper.save(friend);
		}
	}
	
	public void update(FriendEntity friend){
		friendmapper.update(friend);
	}
	
	public void delete(Long id){
		friendmapper.delete(id);
	}
	
	public void deleteBatch(Long[] ids){
		friendmapper.deleteBatch(ids);
	}
	
	public List<FriendEntity> queryListByUser(Long userId) {
		Map<String, Object> map = new HashMap<>();
		map.put("userId", userId);
		return friendmapper.queryListByUser(map);
	}

	public void setFriend(FriendEntity friend) {
		UserEntity userEntity = userService.queryObject(String.valueOf(friend.getFriendUserId()));
		userEntity.setPassword(null);
		userEntity.setCreateTime(null);
		friend.setUser(userEntity);
	}

	public void setFriend(List<FriendEntity> friends) {
		for (FriendEntity friend: friends) {
			setFriend(friend);
		}
	}

	public FriendEntity queryByFriend(FriendApplyEntity friendApplyEntity) {
		FriendEntity friend = new FriendEntity();
		friend.setUserId(friendApplyEntity.getUserId());
		friend.setFriendUserId(friendApplyEntity.getApplyUserId());
		return queryByFriend(friend);
	}

	public FriendEntity queryByFriend(FriendEntity friend) {
		return friendmapper.queryByFriend(friend);
	}
	
}
